package src.main.java.com.lyy.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;

public class CombineClient
{
    public static void main(String[] args)
    {
        try {
            Socket socket = new Socket("localhost", 12345); // 连接服务器12345
            System.out.println("请求连接服务端");
            System.out.println("已连接服务器");
            Scanner input= new Scanner(System.in);
            PrintStream out = new PrintStream(socket.getOutputStream());
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            //说明根据输入字母选择客户端操作
            System.out.println("选择操作 ('m' for message, 'f' for file, 'Exit' to quit):");
            while(true)
            {

                String s = input.next();
                System.out.println(s);
                if(s.equals("m"))
                {
                    out.println(s);
                    System.out.print("请输入消息：");
                    s = input.next();//用户输入信息
                    out.println(s);
                    System.out.println(in.readLine());

                }
                if(s.equals("f"))
                {
                    out.println(s);
                    System.out.print("请输入文件路径：");
                    String filePath;
                    filePath=input.next();//用户输入需要传输的文件路径
                    try
                    {
                        File file = new File(filePath);
                        if (!file.exists()) //若文件未找到，给出说明
                        {
                            System.out.println("文件未找到！" );
                            return;
                        }

                        // 获取文件扩展名
                        String extension = "";
                        int dotIndex = filePath.lastIndexOf('.');
                        if (dotIndex >= 0)
                        {
                            extension = filePath.substring(dotIndex); // 获取文件扩展名
                        }
                        out.println(extension);// 发送文件扩展名给服务器
                        FileInputStream fileInputStream = new FileInputStream(file);
                        OutputStream outputStream = socket.getOutputStream();
                        byte[] buffer = new byte[4096];
                        int bytesRead;
                        while ((bytesRead = fileInputStream.read(buffer)) != -1)
                        {
                            outputStream.write(buffer, 0, bytesRead);
                        }

                        //  fileInputStream.close();//关闭文件流
                        //   outputStream.close();
                        // socket.close();
                        System.out.println("文件传输成功!");
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
                if(s.equals("Exit")) //若客户端选择退出
                {
                    out.println(s);
                    System.out.println(in.readLine());
                    socket.close();//关闭socket
                }

            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}